package com.example.com.common.widget.diffui;

import android.support.v7.util.DiffUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ChenYiYao on 2018/2/28.
 */

public class DiffUiDataCallBack<T extends DiffUiDataCallBack.UidataDiffer<T>> extends DiffUtil.Callback {
    protected List<T> mOldList, mNewList;


    public DiffUiDataCallBack(List<T> oldList, List<T> newList) {
        this.mOldList = oldList;
        this.mNewList = newList;
    }

    public DiffUiDataCallBack() {
        this(new ArrayList<T>(), new ArrayList<T>());
    }

    public void setData(List<T> oldList, List<T> newList) {
        this.mOldList = oldList;
        this.mNewList = newList;
    }

    /**
     * 旧数据的大小
     *
     * @return
     */
    @Override
    public int getOldListSize() {
        return mOldList.size();
    }

    /**
     * 新数据的大小 widget
     *
     * @return
     */
    @Override
    public int getNewListSize() {
        return mNewList.size();
    }

    /**
     * 该方法判断是否同一个对象，或者同一个数据。
     *
     * @param oldItemPosition
     * @param newItemPosition
     * @return 返回false, 回继续调用areContentsTheSame
     */
    @Override
    public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
        T beanOld = mOldList.get(oldItemPosition);
        T beanNew = mNewList.get(newItemPosition);
        return beanNew.isSame(beanOld);
    }

    /**
     * 经过areItemsTheSame方法判断后，再进行对比具体的信息是否改变
     *
     * @param oldItemPosition
     * @param newItemPosition
     * @return
     */
    @Override
    public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
        T beanOld = mOldList.get(oldItemPosition);
        T beanNew = mNewList.get(newItemPosition);
        return beanNew.isDataContentSame(beanOld);
    }

    public interface UidataDiffer<T> {
        // 传递一个旧的数据，用以判断是否同一个数据
        boolean isSame(T old);

        // 判断旧数据和新数据内容是否相同
        boolean isDataContentSame(T old);
    }


}
